package com.woniuxy.microshoppingcartserver.bean;

import lombok.AllArgsConstructor;

import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
public class Cart implements Serializable {
    private List<CartItem> items;
    //商品总数
    private Integer nums;
    private Integer type;//商品类型数

    //购物车里所有商品的总价
    private BigDecimal totalAmount;
    //商品减免价格
    private BigDecimal reduce = new BigDecimal("0.00");

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getNums() {
        if(items != null && items.size()>0){
            items.forEach(e->{
                this.nums += e.getNums();
            });
        }

        return this.nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    public Integer getType() {
        type =0;
        if(items != null && items.size()>0){
            return items.size();
        }
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0.00");
        //计算购物车总价
        if(items != null && items.size()>0){
            for (CartItem item : items
                 ) {
                BigDecimal totalPrice = item.getTotalPrice();
                amount = amount.add(totalPrice);
            }
        }
        //减去优惠价
        BigDecimal substract = amount.subtract(getReduce());
        return substract;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }
}
